from flask import Blueprint,jsonify,request
from utils.comm import get_data,xtree
from db import db1
import datetime


bp_organization = Blueprint('organization',__name__)


@bp_organization.route('/organizationlist',methods=['GET','POST'])
def organizationlist():
    if request.method == 'GET':
        sql = "select id,name,pid from organization where pid=0"
        res = db1.find_all(sql)
        return jsonify({'code':200,'list':res})

    else:
        data = get_data()
        time = datetime.datetime.strftime(datetime.datetime.now(),'%y-%m-%d')
        sql = "insert into organization(name,type,coding,pid,startuser,starttime) values ('%s',%d,'%s',%d,%d,'%s')"%\
              (data['name'],int(data['type']),data['coding'],int(data['pid']),int(data['user_id']),time)
        pass


@bp_organization.route('/list',methods=['GET'])
def list():
    sql = "select * from organization"
    res = db1.find_all(sql)
    data = xtree(res)
    return jsonify({'code': 200, 'list': data})